// 收藏相关API接口
import axios from 'axios'

// 创建axios实例
const api = axios.create({
  baseURL: '/api',
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
})

// 请求拦截器
api.interceptors.request.use(
  config => {
    const token = localStorage.getItem('token')
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
api.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    console.error('API请求错误:', error)
    return Promise.reject(error)
  }
)

// 收藏接口
export interface CollectionItem {
  id: number
  userId: number
  username: string
  contentType: 'QUESTION' | 'FORUM' | 'RESOURCE' | 'ARTICLE'
  contentId: number
  contentTitle: string
  contentDescription?: string
  contentUrl?: string
  collectTime: string
  createdAt: string
}

export interface CollectionResponse {
  content: CollectionItem[]
  totalElements: number
  totalPages: number
  size: number
  number: number
}

// 添加收藏
export const addCollection = async (data: {
  userId: number
  contentType: string
  contentId: number
  contentTitle: string
  contentDescription?: string
  contentUrl?: string
}): Promise<CollectionItem> => {
  return await api.post('/collections', data)
}

// 取消收藏
export const removeCollection = async (userId: number, contentType: string, contentId: number): Promise<void> => {
  return await api.delete(`/collections/user/${userId}/content/${contentType}/${contentId}`)
}

// 检查是否已收藏
export const isCollected = async (userId: number, contentType: string, contentId: number): Promise<boolean> => {
  return await api.get(`/collections/user/${userId}/check/${contentType}/${contentId}`)
}

// 获取用户收藏列表
export const getUserCollections = async (userId: number, params: {
  page?: number
  size?: number
}): Promise<CollectionResponse> => {
  return await api.get(`/collections/user/${userId}`, { params })
}

// 根据类型获取收藏列表
export const getUserCollectionsByType = async (userId: number, contentType: string, params: {
  page?: number
  size?: number
}): Promise<CollectionResponse> => {
  return await api.get(`/collections/user/${userId}/type/${contentType}`, { params })
}

// 搜索收藏
export const searchUserCollections = async (userId: number, keyword: string, params: {
  page?: number
  size?: number
}): Promise<CollectionResponse> => {
  return await api.get(`/collections/user/${userId}/search`, { 
    params: { keyword, ...params } 
  })
}

// 删除收藏
export const deleteCollection = async (collectionId: number): Promise<void> => {
  return await api.delete(`/collections/${collectionId}`)
}

// 批量删除收藏
export const batchDeleteCollections = async (userId: number, collectionIds: number[]): Promise<void> => {
  return await api.delete(`/collections/user/${userId}/batch`, { data: collectionIds })
}

// 清空用户收藏
export const clearUserCollections = async (userId: number): Promise<void> => {
  return await api.delete(`/collections/user/${userId}/clear`)
}

// 清空特定类型的收藏
export const clearUserCollectionsByType = async (userId: number, contentType: string): Promise<void> => {
  return await api.delete(`/collections/user/${userId}/type/${contentType}/clear`)
}
